package com.event.controller;

import com.event.pojo.Category;
import com.event.pojo.Result;
import com.event.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
        try{
            categoryService.add(category);
            return Result.success("添加成功");
        }catch(Exception e){
            return Result.success("添加失败");
        }
    }

    @GetMapping("/queryCategoryList")
    public Result<List<Category>> queryCategoryList() {
        List<Category> categoryList = categoryService.queryList();
        return Result.success(categoryList);
    }

    @GetMapping("/detail")
    public Result<Category> detail(@RequestParam @Validated Integer id) {
        Category category =  categoryService.queryDetail(id);
        return Result.success(category);
    }

    @PutMapping("/updateCategory")
    public Result updateCategory(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success("更新成功");
    }

    @DeleteMapping("/deleteCategory")
    public Result deleteCategory(@RequestParam @Validated(Category.Delete.class)  Integer id){
        categoryService.deleteCategory(id);
        return Result.success("更新成功");
    }
}
